package com.example.springlearn.util.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * redis list 工具类
 */
@Slf4j
@Component
public class ListUtil {

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Autowired
    ToolsUtil toolsUtil;

    /**
     * 获取list缓存的内容
     *
     * @param key   键
     * @param start 开始
     * @param end   结束  0 到 -1代表所有值
     */
    public List<Object> lGet(String key, long start, long end) {
        try {
            return redisTemplate.opsForList().range(key, start, end);
        } catch (Exception e) {
            log.warn(e.toString());
            return null;
        }
    }

    /**
     * 获取list缓存的长度
     *
     * @param key 键
     */
    public long lGetListSize(String key) {
        try {
            return redisTemplate.opsForList().size(key);
        } catch (Exception e) {
            log.warn(e.toString());
            return 0;
        }
    }

    /**
     * 通过索引 获取list中的值
     *
     * @param key   键
     * @param index 索引
     */
    public Object lGetIndex(String key, long index) {
        try {
            return redisTemplate.opsForList().index(key, index);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 将 list 中的全部值 放入缓存
     *
     * @param key   键
     * @param value 值的集合
     * @return
     */
    public boolean lSet(String key, Object ...value) {
        for (Object o : value) {
            log.info("{}", o);
        }
        try {
            if (value.length == 0) {
                return false;
            } else if (value.length == 1) {
                redisTemplate.opsForList().rightPush(key, value[0]);
            } else {
                redisTemplate.opsForList().rightPushAll(key, value);
            }

            return true;
        } catch (Exception e) {
            log.warn(e.toString());
            return false;
        }

    }

    /**
     * 将 值 放入缓存 数组/列表
     *
     * @param key   键
     * @param value 值
     * @param time  时间(秒)
     */
    public boolean lSet(long time, String key, Object ...value) {
        try {
            if (value.length == 0) {
                return false;
            } else if (value.length == 1) {
                redisTemplate.opsForList().rightPush(key, value[0]);
            } else {
                redisTemplate.opsForList().rightPushAll(key, value);
            }

            if (time > 0) {
                return toolsUtil.expire(key, time);
            }
            return true;
        } catch (Exception e) {
            log.warn(e.toString());
            return false;
        }
    }


    /**
     * 根据索引修改list中的某条数据
     *
     * @param key   键
     * @param index 索引
     * @param value 值
     * @return
     */
    public boolean lUpdateIndex(String key, long index, Object value) {
        try {
            redisTemplate.opsForList().set(key, index, value);
            return true;
        } catch (Exception e) {
            log.warn(e.toString());
            return false;
        }
    }

    /**
     * 移除N个值为value
     *
     * @param key   键
     * @param count 移除多少个
     * @param value 值
     * @return 移除的个数
     */
    public long lRemove(String key, long count, Object value) {
        try {
            Long remove = redisTemplate.opsForList().remove(key, count, value);
            return remove;
        } catch (Exception e) {
            log.warn(e.toString());
            return 0;
        }
    }



}
